import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/controller/knowledgeword.dart';
import 'package:ichinesereader/controller/schooladmin.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/cards/word.dart';
import 'package:ichinesereader/widgets/buttons/wordButtonBar.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/widgets/buttons/FloatingActionButtonRecycleSwitch.dart';

List<GlobalKey<WordCardState>> wordCardKeys = [];
bool disableWordCardAudioPlay = false;

class MyKnowledgeWordsPage extends StatefulWidget {
  MyKnowledgeWordsPage({Key? key, this.folderId, this.level, this.isMainFolder})
      : super(key: key);
  final String? folderId;
  final String? level;
  final bool? isMainFolder;
  static const String routeName = '/common/mywords';

  @override
  State<MyKnowledgeWordsPage> createState() => _MyKnowledgeWordsPageState();
}

class _MyKnowledgeWordsPageState extends State<MyKnowledgeWordsPage> {
  final KnowledgeWordsController _controller =
      Get.put(KnowledgeWordsController());
  final box = Hive.box('appconfig');

  @override
  void initState() {
    Future.delayed(Duration(milliseconds: 600), () {
      _controller.fetchKnowledgeFolderTree(false);
    });
    super.initState();
  }

  @override
  void dispose() {
    Get.delete<KnowledgeWordsController>();
    Get.delete<SchoolAdminController>();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('Build common my knowledge words page');

    bool isTeacher = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Teacher;
    // bool isStudent = box.get(CacheManagerKey['nanhairole'].toString()) ==
    //     Constants.NanhaiRole_Student;
    // bool isIndividual = box.get(CacheManagerKey['nanhairole'].toString()) ==
    //     Constants.NanhaiRole_Individual;

    // dynamic _getAppDrawer() {
    //   if (isTeacher) return null;
    //   if (isStudent) return StudentAppDrawer();
    //   if (isIndividual) return IndividualAppDrawer();
    // }

    return Scaffold(
      backgroundColor: Colors.transparent,
      body: _controller.obx((data) {
        return Column(children: [
          Container(
            margin: EdgeInsets.all(
              12.0,
            ),
            child: WordButtonBar(isTeacher: isTeacher),
          ),
          Expanded(
            child: _controller.obx(
              (data) {
                wordCardKeys = [];
                return GridView.builder(
                    gridDelegate:
                        const SliverGridDelegateWithMaxCrossAxisExtent(
                            maxCrossAxisExtent: 300,
                            childAspectRatio: 156 / 200,
                            crossAxisSpacing: 12,
                            mainAxisSpacing: 12),
                    itemCount: _controller.mainMode.value
                        ? _controller.knowledgeWords.length
                        : _controller.recycleWords.length,
                    itemBuilder: (context, index) {
                      var ckey = GlobalKey<WordCardState>();

                      if (_controller.knowledgeWords[index].id != null &&
                          _controller.knowledgeWords[index].id != '')
                        wordCardKeys.add(ckey);

                      return WordCard(
                        flashcard: _controller.mainMode.value
                            ? _controller.knowledgeWords[index]
                            : _controller.recycleWords[index],
                        key: ckey,
                      );
                    });
              },
              onEmpty: Center(child: Text('No cards or folders'.tr)),
              onLoading: Center(
                child: CircularProgressIndicator(
                  color: Get.isDarkMode
                      ? Colors.white
                      : Get.theme.primaryColorDark,
                  backgroundColor: Get.isDarkMode
                      ? Get.theme.primaryColorDark.withOpacity(0.3)
                      : Get.theme.primaryColorDark.withOpacity(0.3),
                  strokeWidth: 9.0,
                ),
              ),
            ),
          ),
        ]);
      }),
      floatingActionButton: FloatingActionButtonRecycleSwitch(),
    );
  }
}
